package com.xunshangwang.pinhaohuo.app;

import android.app.Application;
import android.content.SharedPreferences;

import com.xunshangwang.pinhaohuo.entity.Account;
import com.xunshangwang.pinhaohuo.http.HttpTool;
import com.xunshangwang.pinhaohuo.http.JsonHttpHandler;
import com.xunshangwang.pinhaohuo.tools.SPHelper;

import cn.sharesdk.framework.ShareSDK;
import tool.library.ToastTool;

/**
 * Created by xzp on 16/1/14.
 */
public class MainApplication extends Application{
    private Account mAccount;
    @Override
    public void onCreate() {
        super.onCreate();
        ToastTool.init(this);
        ShareSDK.initSDK(this);
        JsonHttpHandler.init(this.getApplicationContext());
        boolean isLogin= SPHelper.getBooleanValue(this,SPHelper.SP_CONFIG,SPHelper.IS_LOGIN);
        if(isLogin){
            mAccount=new Account();
            SharedPreferences preferences=getSharedPreferences(SPHelper.SP_ACCOUNT,MODE_PRIVATE);
            mAccount.setHeadimgurl(preferences.getString("headimgurl",null));
            mAccount.setNickname(preferences.getString("nickname", null));
            mAccount.setSex(preferences.getString("sex", "男"));
            mAccount.setSubscribe(preferences.getString("subscribe", null));
            mAccount.setUnionid(preferences.getString("unionid", null));
            mAccount.setUser_sn(preferences.getString("user_sn", null));
            mAccount.setToken(preferences.getString("token",null));
        }
    }
    public final boolean isLogin(){
        return mAccount!=null;
    }

    public final String getToken(){
        if(mAccount==null)
            return null;
        return mAccount.getToken();
    }
    public final Account getAccount(){
        return mAccount;
    }
    public final void setAccount(Account account){
        mAccount=account;
        if(account==null) {
            SharedPreferences.Editor configEditor=getSharedPreferences(SPHelper.SP_CONFIG,MODE_PRIVATE).edit();
            configEditor.putBoolean(SPHelper.IS_LOGIN, false);
            configEditor.commit();
            SharedPreferences.Editor accountEditor=getSharedPreferences(SPHelper.SP_ACCOUNT,MODE_PRIVATE).edit();
            accountEditor.clear();
            accountEditor.commit();
        }
        else {
            SharedPreferences.Editor configEditor=getSharedPreferences(SPHelper.SP_CONFIG,MODE_PRIVATE).edit();
            configEditor.putBoolean(SPHelper.IS_LOGIN, true);
            configEditor.commit();
            SharedPreferences.Editor accountEditor=getSharedPreferences(SPHelper.SP_ACCOUNT,MODE_PRIVATE).edit();
            accountEditor.putString("headimgurl",account.getHeadimgurl());
            accountEditor.putString("nickname",account.getNickname());
            accountEditor.putString("sex",account.getSex());
            accountEditor.putString("subscribe",account.getSubscribe());
            accountEditor.putString("unionid",account.getUnionid());
            accountEditor.putString("user_sn",account.getUser_sn());
            accountEditor.putString("token",account.getToken());
            accountEditor.commit();
        }
    }
}
